package chaoyue.study.service;

import chaoyue.study.domain.LoginUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * UserDetailsService 是SpringSecurity提供的接口
 * 目的是根据用户名查询用户信息，用来做认证校验
 * 改造的时候一般这边从数据库中去进行用户信息的查询，这边直接hardCode做演示
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username.equals("chaoyue")) {
            // hardCode权限信息 正常应该查询数据库获取
            List<String> authList = List.of("normal","root");
            // 容器中注入了BCryptPasswordEncoder 会使用这个来进行密码加密
            return new LoginUser(username, "$2a$10$E5nCDjnGePDpe9K0TC0OHuqj1bU5FHjlnFeYPbKQsR5QN53G/f98W", authList);
        } else {
            throw new RuntimeException("用户不存在！");
        }
    }
}
